package com.service.impl;

import com.domain.Blog;
import com.mapper.BlogMapper;
import com.service.BlogService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class BlogServiceImpl implements BlogService {
    @Resource
    private BlogMapper blogMapper;
    //获取所有Blog分页数据,并处理显示长度：50字符
    public List<Blog> findAllBlog(){
        List<Blog> blogs = blogMapper.findAllBlog();
        for (Blog blog : blogs){
            if (blog.getBcontent().length() > 120){
                blog.setBcontent(blog.getBcontent().substring(0,120)+"...");
            }
        }
        return blogs;
    }
    //获取hot前五的Blog（访问量）
    public List<Blog> findHotBlog(){
        return blogMapper.findHotBlog();
    }
    //类型查找
    public List<Blog> findBlogByType(int checkType) {
        return blogMapper.findBlogByType(checkType);
    }
    //关键字查找（内容）
    public List<Blog> findBlogByKeyWords(String keyWords) {
        return blogMapper.findBlogByKeyWords(keyWords);
    }
    //根据id获取Blog,访问量+1
    @Transactional
    public Blog findBlogById(Integer id){
        blogMapper.addPageView(id);
        return blogMapper.findBlogById(id);
    }
    //根据标题查找
    public List<Blog> findBlogByTitle(String keyWords) {
        return blogMapper.findBlogByTitle(keyWords);
    }
    //删除blog
    @Transactional
    public int deleteblog(Integer bid) {
        return blogMapper.deleteblog(bid);
    }
    //发布blog
    @Transactional
    public int insertBlog(Blog blog) {
        return blogMapper.insertBlog(blog);
    }
    //更新blog前按id查找该blog
    public Blog selectBlogById(int id){
        return blogMapper.findBlogById(id);
    }
    //更新blog
    @Transactional
    public int updateBlog(Blog blog){
        return blogMapper.updateBlog(blog);
    }
    //写入关联数据到t_b_t表
    @Transactional
    public int insertTBT(Map map) {
        return blogMapper.insertTBT(map);
    }
    //删除关联关系
    @Transactional
    public int deleteTBT(int bid) {
        return blogMapper.deleteTBT(bid);
    }


    //判断关联数据是否存在
//    public boolean isExist(Map map){
//        int flag = blogMapper.isExist(map);
//        if (flag > 0){
//            return true;
//        }else{
//            return false;
//        }
//    }


    //    public String dateToString(Date date){
//        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
//    }
}
